iT邦幫忙

2019 iT 邦幫忙鐵人賽

DAY 19
0

Day nineteen

How to use 'git branch'?
如何使用git支分功能?

Hello everyone. It's Ray.
大家好,我是Ray!

In my last article, I initially introduced the concept and function of git branch, so today let's get our hands dirty to obtain this skill.
上一篇跟大家做了git分支的概念以及功能介紹,今天讓我們通過實作來掌握這一項實用的技能。

At the first, let's take a look on the history.
首先呢,那我們來確認一下我們的歷史

If you've been following along our articles, it should look like photo above.
如果大家有跟著我們之前文章的進度走的話,我們的歷史紀錄應該會跟上圖一樣。

Let's enter:
來我們輸入以下code:

git tag -a v2.0 "stable version 2"
git tag -n
git log --oneline

Hypothetically, we've completed current function and made a commit for it. Then we would like to develop another function, so we could create another branch.
假設我們已經完成目前的功能,並且commit保存,然後我們想要在開發另一項功能,我們可以另開一個分支。

Enter:
請輸入:

git branch test
git branch

As photo above, we've created a new branch.
如上圖,我們已經新增了分支。

Now let's switch to this branch.
現在讓我們切到分支去

Enter:
輸入:

git checkout test
git log --oneline

As photo above, we could see that currently the HEAD is pointing to test, which means that we are on test branch.
如上圖,可以看到'HEAD'目前指向test,這代表目前我們已經處於test分支。

Now let's add a new description 'This is a new function after stable reversion 2' in example2.html, as follows:
現在讓我們新增一行code,'This is a new function after stable reversion 2'到example2.html,如下:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
<p>This is the experimental file created after reversion v1.0</p>
<p>This is a wrong commit</p>
<p>This is a example description for further git revert explanation</p>
<p>This is a new function after stable reversion 2</p>
</body>
</html>

Enter:
輸入:

git commit -am "a new function after stable reversion 2"
git log --oneline

As photo above, we could see that we've made a new commit on test branch.
如上圖可以看到我們已經在新的分支上做了一個新的commit了!

If we visualize git, it should probably look like image below:
如果我們視覺化git目前的狀況,大概會是像下面的圖片:

Now assume that we need to go back to master branch to do some test, hwo?
現在假設我們需要回到master主線去做一些測試,該怎麼回去呢?

Enter:
輸入:

git checkout master
git log --oneline

As image above, now we are on master branch, and what we added specifically on test branch is gone.
如上圖,我們已經回到master分支,並且剛剛在test分支新增的code再切換回去master分支之後就不見了!

Okay. Let's delete test branch before calling it for today.
在今天的文章結束之前,讓我們把剛剛建立的分支做刪除:

git branch -D test

I hope this article is useful to you guys. See you tomorrow.
希望今天的文章對大家理解git有所幫助,我們明天見!


上一篇
git branch,想怎麼開發就怎麼開發
下一篇
git branch - 回到過去開個分支,開始新的人生
系列文
Experience of a backend novice30
圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

尚未有邦友留言

立即登入留言